part of '../widgets.dart';



///
class VanRadioGroup extends StatelessWidget with VanProvide {
  /// props - 选中的值
  final VanCtrl controller;

  /// props - 是否禁用所有单选框(默认: false)
  final bool disabled;

  /// props - 排列方向，可选值为 horizontal(默认: vertical)
  final VanRadioDirection direction;

  /// props - 所有单选框的图标大小，默认单位为 px(默认: 20px)
  final double iconSize;

  /// type: [Color] or [Gradient] <br>
  /// props - 所有单选框的选中状态颜色(默认: #1989fa)
  final Color checkedColor;

  /// props - 形状，可选值为 square dot(默认: round)
  final VanRadioShape shape;

  /// props - 初始化数据列表
  final int itemCount;

  /// events - 当绑定值变化时触发的事件 回调参数(value)
  final Function(dynamic)? onChange;

  /// slots - 默认插槽 - 单组件 转 多组件
  /// <br>type: [String] or [Widget]
  final dynamic child;

  /// slots - 默认插槽 - 多子组件
  final List<Widget>? children;

  VanRadioGroup({
    super.key,
    dynamic value,
    VanCtrl? controller,
    this.disabled = false,
    this.direction = VanRadioDirection.vertical,
    this.iconSize = 20,
    this.checkedColor = const Color(0xff1989fa),
    this.shape = VanRadioShape.round,
    this.itemCount = 0,
    this.onChange,
    this.child,
    this.children,
  }) : controller = controller ?? VanCtrl(value);

  @override
  Widget build(BuildContext context) {
    if (onChange != null) controller.addListener(() => onChange!(controller.value!));

    List<Widget> ws;
    Widget? widget;

    if (child is String) {
      widget = Text(child);
    } else if (child is Widget) {
      widget = child;
    } else if (child != null){
      _logger.e("VanRadioGroup.child only type support [String] or [Widget]");
    }

    if (widget != null) {
      ws = [widget, ...children ?? []];
    } else {
      ws = children ?? [];
    }

    if (direction == VanRadioDirection.vertical) {
      widget = Column(children: ws);
    } else {
      widget = Row(children: ws);
    }
    return VanProvider(provide: this, child: widget);
  }
}

@immutable
class VanRadioState {
  final bool checked;
  final bool disabled;
  final int index;

  const VanRadioState({this.checked = false, this.disabled = false, required this.index});
}

typedef VanRadioGroupBuilder = Widget Function(VanRadioState state);

class VanRadioGroupController<T> extends VanCtrl<T> {
  VanRadioGroupController(super.value);
}